package weaver.jobs.cyjt.duss.cron;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import weaver.common.DateUtil;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import weaver.jobs.cyjt.duss.utils.MchrUtil;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author dusongsong
 * @date 2024年10月08日 09:30
 */
public class WeaUpdateMCHRInfoCronJob extends BaseCronJob {
    @Override
    public void execute() {
        MchrUtil mchrUtil = new MchrUtil();
        BaseBean baseBean = new BaseBean();
        try {
            baseBean.writeLog(">>>>>WeaUpdateMCHRInfoCronJob>>>>>curdate=" + DateUtil.getCurrentDate());
            String dataStr = mchrUtil.getFlowCreateURLList();
            baseBean.writeLog(">>>>>WeaUpdateMCHRInfoCronJob>>>>>dataStr=" + dataStr);
            if (!"".equals(dataStr)) {
                if (mchrUtil.isJSONArray(dataStr)) {
                    String ids = mchrUtil.isExistFlowData();
                    JSONArray jsonArray = JSONArray.parseArray(dataStr);
                    for (int i = 0; i < jsonArray.size(); i++) {
                        Map<String, Object> map = new HashMap<>();
                        JSONObject dataJson = jsonArray.getJSONObject(i);
                        String id = Util.null2String(dataJson.getString("ID"));
                        map.put("outkey", id);
                        map.put("title", Util.null2String(dataJson.getString("TITLE")));
                        map.put("linkurl", Util.null2String(dataJson.getString("LINKURL")));
                        map.put("flowversion", Util.null2String(dataJson.getString("FLOWVERSION")));
                        map.put("status", "0");
                        List<String> idsList = Arrays.asList(ids.split(","));
                        if (idsList.contains(id)) {
                            map.remove("outkey");
                            mchrUtil.updateModelData("uf_mchrlcpzb", id, map);
                        } else {
                            mchrUtil.insertModelData("uf_mchrlcpzb", map);
                        }
                    }
                }
            }
        } catch (Exception e) {
            baseBean.writeLog("WeaUpdateMCHRInfoCronJob", e);
            e.printStackTrace();
        }

    }
}
